package com.ruoyi.vote.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.vote.admin.service.IWxSubTampService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.vote.admin.domain.bo.WxSubTampBo;
import com.ruoyi.vote.admin.domain.vo.WxSubTampVo;
import com.ruoyi.vote.admin.domain.WxSubTamp;
import com.ruoyi.vote.admin.mapper.WxSubTampMapper;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 微信订阅消息模板Service业务层处理
 *
 * @author 筑巢
 * @date 2023-07-25
 */
@RequiredArgsConstructor
@Service
public class WxSubTampServiceImpl implements IWxSubTampService {

    private final WxSubTampMapper baseMapper;

    /**
     * 查询微信订阅消息模板
     */
    @Override
    public WxSubTampVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 根据模块查询模板
     *
     * @param model
     */
    @Override
    public WxSubTampVo queryByModel(String model) {
        return baseMapper.selectVoOne(new LambdaQueryWrapper<WxSubTamp>()
            .eq(WxSubTamp::getTempModel,model));
    }

    /**
     * 查询微信订阅消息模板列表
     */
    @Override
    public TableDataInfo<WxSubTampVo> queryPageList(WxSubTampBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WxSubTamp> lqw = buildQueryWrapper(bo);
        Page<WxSubTampVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询微信订阅消息模板列表
     */
    @Override
    public List<WxSubTampVo> queryList(WxSubTampBo bo) {
        LambdaQueryWrapper<WxSubTamp> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WxSubTamp> buildQueryWrapper(WxSubTampBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WxSubTamp> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTampId()), WxSubTamp::getTampId, bo.getTampId());
        lqw.eq(bo.getTampNumber() != null, WxSubTamp::getTampNumber, bo.getTampNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), WxSubTamp::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), WxSubTamp::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增微信订阅消息模板
     */
    @Override
    public Boolean insertByBo(WxSubTampBo bo) {
        WxSubTamp add = BeanUtil.toBean(bo, WxSubTamp.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改微信订阅消息模板
     */
    @Override
    public Boolean updateByBo(WxSubTampBo bo) {
        WxSubTamp update = BeanUtil.toBean(bo, WxSubTamp.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WxSubTamp entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除微信订阅消息模板
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
